<?php

namespace App\Admin\Controllers;

use App\Models\CaseCase;
use App\Admin\Renderable\CaseBatchTable;
use App\Admin\Renderable\UserTable;
use App\Admin\Traits\Customfields;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Admin;
use App\Models\CaseBatch;
use App\Admin\Traits\ChangeUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use Dcat\Admin\Http\Controllers\AdminController;

class CaseCaseController extends AdminController
{
    use Customfields,ChangeUser;

    public static $css = [
        '/static/css/case_show.css',
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(CaseCase::with(['adminUser', 'caseBatch', 'caseBatch.caseCustomer']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title')->link(function () {
                return admin_url('casecases/' . $this->id);
            });
            $grid->column('case_batch.number','委托批次号');
            $grid->column('caseBatch.caseCustomer.name','所属客户');
            $grid->column('adminUser.name','负责律师');

            $grid->column('trial_num')->using(
                [
                    1 => '<span class="badge badge-success">一审</span>',
                    2 => '<span class="badge badge-warning">二审</span>',
                    3 => '<span class="badge badge-info">仲裁</span>',
                ]
            );
            $grid->column('trial_step')->using(
                [
                    0 => '待立案',
                    1 => '待缴费',
                    2 => '待传票',
                    3 => '待判决',
                    4 => '待结案',
                    5 => '已调解',
                    6 => '申请执行',
                    7 => '立案执行',
                    8 => '撤案',
                    9 => '已结案',
                ]
            );
            $this->gridfield($grid, 'case');
            $grid->model()->orderBy('id', 'desc');
            $grid->column('created_at')->sortable();
            // $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public function show($id, Content $content)
    {
        Admin::css(static::$css);
        $case = CaseCase::with(['caseBatch.caseCustomer.caseContacts','caseBatch.caseContract','caseProgress' => function ($q) {
            $q->orderBy('id', 'desc');
        }, 'caseEvents' => function ($q) {
            $q->orderBy('updated_at', 'desc');
        },'caseEvents.caseContact','caseEvents.adminUser','caseReceipts', 'attachments'])->findorFail($id);


        $data = [
            'case'       => $case,
            'caseprogress'       => $case->caseProgress,
            'casebatch'       => $case->caseBatch,
            'contacts' => $case->caseBatch->caseCustomer->caseContacts,
            'contract' => $case->caseBatch->caseContract,
            'events'         => $case->caseEvents,
            'customer'       => $case->caseBatch->caseCustomer,
            'adminUser' => $case->adminUser,
            'casefields' => $this->custommodel('case'),
            'receipts' => $case->caseReceipts,
            'attachments'    => $case->attachments,
            'Change' => $this->Change($id,'case'),
            // 'shares_user' => $customer->SharesUser()->select(['name', 'avatar'])->get(),
        ];
        // dd($case->caseProgress->count());
        return $content
            ->title('案件')
            ->description('详情')
            ->body($this->_detail($data));
    }

    private function _detail($data)
    {
        return view('admin/case/show', $data);
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CaseCase(), function (Form $form) {
            $form->display('id');
            $form->text('title');

            $form->selectTable('case_batch_id')
            ->title('选择所属委托')
            ->dialogWidth('50%') // 弹窗宽度，默认 800px
            ->from(CaseBatchTable::make(['id' => $form->getKey()])) // 设置渲染类实例，并传递自定义参数
            ->model(CaseBatch::class, 'id', 'number'); // 设置编辑数据显示

            $form->selectTable('admin_user_id')
            ->title('选择当前案件负责律师')
            ->dialogWidth('50%') // 弹窗宽度，默认 800px
            ->from(UserTable::make(['id' => $form->getKey()])) // 设置渲染类实例，并传递自定义参数
            ->model(Administrator::class, 'id', 'name')
            ->default(Admin::user()->id); // 设置编辑数据显示

            $form->radio('trial_num')->options([1 => '一审', 2=> '二审'])->default(1);
            $form->radio('trial_step')->options([0 => '待立案', 1=> '待缴费', 2=> '待传票', 3=> '待判决', 4=> '待结案', 5=> '已调解', 9=> '已结案'])->default(0);
            $form->hidden('fields')->value(null);
            $form->hidden('receipt')->value(0);
            $form->hidden('salesexpenses')->value(0);
            $this->formfield($form, 'case');
            $form->display('created_at');
            $form->display('updated_at');
            $class = $this;
            $form->saving(function (Form $form) use ($class) {
                $form_field = array();
                foreach ($class->custommodel('case') as $field) {
                    $field_field = $field['field'];
                    $form_field[$field_field] = $form->$field_field;
                    $form->deleteInput($field['field']);
                }
                $form->fields = json_encode($form_field);

                return $form;
            });
        });
    }


    protected function changeUser(CaseCase $case,Request $request)
    {
        $request->validate([
            'userid' => 'required|digits_between:1,3)'
        ]);
        $case->update([
            'admin_user_id' => $request->userid,
        ]);
        admin_toastr('更新成功', 'success');
        return redirect()->route('dcat.admin.casecases.show', $case->id);
    }
}
